<?php
include("../include/db.php");
include("includes/thumbnail.php");

$_SESSION['rpage'] = $_SERVER['REQUEST_URI']; 
if(!isset($_SESSION[$prefix."_username"])){header("location:../login/");exit;}
if ($_SESSION[$prefix."_security"] < 10){header("location:../login/?error=2"); exit;}

$year = date("Y");
$date = "03/18/2011";

$action = $_GET['action'];
$user	= $_GET['user'];
$site	= $_GET['site'];

if($action == 'access'){
	$result1 = mysql_query("DELETE FROM film_cat WHERE film_cat_id = '$site'", $db);
}

if($action == 'screening'){
	$result1 = mysql_query("DELETE FROM screenings WHERE screen_id = '$site'", $db);
}


if ($_SERVER['REQUEST_METHOD'] == "POST") { 
	$id			= $_POST[id];
	$title		= $_POST[title];
	$title_alpha= $_POST[title_alpha];
	$director	= $_POST[director];
	$time		= $_POST[runtime];
	$year		= $_POST[year];
	$country	= $_POST[country];
	$pictures	= $_POST[pics];
	$tickets	= $_POST[tickets];
	$video		= $_POST[video];
	$desc		= $_POST[editor1];
	$cat  		= $_POST[cat];
	$delete		= $_POST[delete];
	$web		= $_POST[web];
	
if($delete == "DELETE"){

	//************************ UPDATE USERS ********************************  
	$query = mysql_query("DELETE FROM screenings WHERE film_id = '$id'", $db);
	//********************************************************************** 
	//************************ UPDATE USERS ********************************  
	$query = mysql_query("DELETE FROM film_cat WHERE film_id = '$id'", $db);
	//********************************************************************** 
	//************************ UPDATE USERS ********************************  
	$query = mysql_query("DELETE FROM films WHERE film_id = '$id'", $db);
	//********************************************************************** 

header("Location: film_list.php?action=deleted"); exit; 
}

	if (($_FILES['mug_file']['name']) != ""){
		$site_pic_size = 400;
		$temp_loc = $_FILES['mug_file']['tmp_name'];
		$tn_name = basename($_FILES['mug_file']['name']);
		$file = "../files/$tn_name";
	$thumbnail = global_resize($temp_loc, $site_pic_size, $tn_name, $file);
		$thumb_sql = " , thumbnail='$thumbnail' ";
	}
	//************************ INSERT INTO COURSES_TRM ********************************  
	$query = "UPDATE films SET title='$title', title_alpha='$title_alpha', description='$desc', country='$country', year_produced='$year', runtime='$time', director='$director', video_embed='$video', pictures='$pictures', ticket_link='$tickets', film_web='$web' $thumb_sql WHERE film_id = '$id'";
	$result = mysql_query($query, $db) or die(mysql_error());
	//********************************************************************************* 

	if($cat != 'none'){
		//************************ INSERT INTO COURSES_TRM ********************************  
		$query = mysql_query("INSERT INTO film_cat (film_id, cat_id) VALUES ('$id', '$cat')", $db);
		//*********************************************************************************
	}


	$venue		= $_POST[venue];
	$date		= $_POST[date];
	$hour		= $_POST[hour];
	$min		= $_POST[min];
	$ampm		= $_POST[ampm];

if($venue != 'none'){
	if($ampm == 'pm'){$hour = $hour + 12;}
	$time = "00:".$hour.":".$min;
	
	$date = date("Y-m-d", strtotime($date));

	//************************ INSERT INTO COURSES_TRM ********************************  
	$query = "INSERT INTO screenings 
				(film_id,
				date,
				time,
				venue_id,
				type)
			 VALUES (
			 	'$id',
			 	'$date',
			 	'$time',
			 	'$venue',
			 	'f'
			 )";
	$result = mysql_query($query, $db) or die(mysql_error());
	//********************************************************************************* 
}

header("Location: film_list.php?action=updated"); exit; 


}//END POST



$in_film = (int)$_GET['id'];

$result = mysql_query("SELECT * FROM films WHERE film_id = '$in_film'  ", $db);
$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="includes/admin.css"/>
<title><?php echo $global_title;?></title>


<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>

<link rel="stylesheet" type="text/css" href="includes/jqueryui.css">
<script> 
	$(function() {
		$( "#date" ).datepicker();
	});
	</script> 

</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="main"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_main.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


	<h1>Edit Film: <?php echo $myrow[title];?></h1>

	<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" >
	<input type="hidden" name="id" value="<?php echo $myrow[film_id];?>" >
		<fieldset><legend>Edit Film</legend>

          <dl>
            <dt>Title:</dt>
            <dd><input type="text" name="title" id="title" required autofocus size="50" value="<?php echo $myrow[title];?>"  /></dd>
          </dl>
          <dl>
            <dt>Alpha Title <small>(used to list the films in alphabetical order)</small>:</dt>
            <dd><input type="text" name="title_alpha" id="title_alpha" required size="50" value="<?php echo $myrow[title_alpha];?>"  /></dd>
          </dl>
          <dl>
            <dt>Director:</dt>
            <dd><input type="text" name="director" id="director"  size="50" value="<?php echo $myrow[director];?>"  /></dd>
          </dl>
          <dl>
            <dt>Running Time (minutes):</dt>
            <dd><input type="number" name="runtime" id="runtime"  value="<?php echo $myrow[runtime];?>"  /></dd>
          </dl>
          <dl>
            <dt>Year Produced:</dt>
            <dd><input type="text" name="year" id="year"  value="<?php echo $myrow[year_produced];?>"   /></dd>
          </dl>
          <dl>
            <dt>Country of Origin:</dt>
            <dd><input type="text" name="country" id="country"  value="<?php echo $myrow[country];?>"   /></dd>
          </dl>
          <dl>
            <dt>Website:</dt>
            <dd><input type="url" name="web" id="web" placeholder="http://www...." size="50"  value="<?php echo $myrow[film_web];?>"   /></dd>
          </dl>
          <dl>
            <dt>Link to Pictures:</dt>
            <dd><input type="url" name="pics" id="pics" placeholder="http://www...." size="50"  value="<?php echo $myrow[pictures];?>"   /></dd>
          </dl>
          <dl>
            <dt>Link to Tickets:</dt>
            <dd><input type="url" name="tickets" id="tickets"  placeholder="http://www...." size="50"  value="<?php echo $myrow[ticket_link];?>"  /></dd>
          </dl>
                    
          <dl>
            <dt>Video Embed Code:</dt>
            <dd><textarea name="video" rows="6" cols="60"><?php echo $myrow[video_embed];?></textarea>
                </dd>
          </dl>
          
          <?php if($myrow[thumbnail] != ''){ ?>
          
          <dl>
            <dt>Current Thumbnail:</dt>
            <dd><img src="<?php echo $myrow[thumbnail];?>"></dd>
          </dl>
          
          <?php } ?>
          
          <dl>
            <dt>Film Thumbnail Upload:</dt>
            <dd><input type="file" name="mug_file" id="mug_file"  /></dd>
          </dl>
          
          <dl>
            <dt>Delete Film:</dt>
            <dd><input type="checkbox" name="delete" value="DELETE"></dd>
          </dl>
          
       </fieldset>
       
       <fieldset><legend>Categories</legend>   
          
         <dl>
         	<dt>Current Categories:</dt>
         	<dd>
          <?php $x = 0;
                    $result = mysql_query("SELECT * FROM film_cat, categories WHERE film_cat.film_id = '$in_film' AND film_cat.cat_id = categories.cat_id  ORDER BY categories.cat_name", $db);
                    	while ($myrow1 = mysql_fetch_array($result, MYSQL_ASSOC)) { 
                    	if($x == 0){$nolist = "'".$myrow1["cat_id"]."'"; $x++;}else{$nolist .= ", '$myrow1[cat_id]'"; $x++; }
                    	echo $myrow1["cat_name"]." - <a href='?id=$in_film&action=access&site=$myrow1[film_cat_id]'>remove</a> <br />";
                    	 }
                    	 if($nolist != ''){$notin = " WHERE cat_id NOT IN ($nolist)";} ?>
				&nbsp;</dd>
			</dl>
			
			<dl>
				<dt>Add Film to Category:</dt>
			
				<dd>
				<select name="cat" id="cat">
                    <option value="none">None</option>
                    
                    <?php
                    
                    $result = mysql_query("SELECT * FROM categories $notin  ORDER BY cat_name", $db);
                    	while ($myrow2 = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
                    	<option value="<?php echo $myrow2["cat_id"];?>" <?php if($myrow2["cat_id"] == $in_cat){echo "selected";}?>><?php echo $myrow2["cat_name"];?></option>

                    	<?php }?>

                  </select>
                  </dd>
			</dl>
		</fieldset>
		
		<?php 
		 $result = mysql_query("SELECT * FROM screenings, venues WHERE screenings.venue_id = venues.venue_id AND screenings.film_id = '$in_film' ORDER BY screenings.date, screenings.time", $db);
		 if(mysql_num_rows($result) != ''){ ?>
		
		
		 <fieldset><legend>Screenings</legend>   
          
         <dl>
         	<dt>Current Screenings:</dt>
         	<dd>
          <?php $x = 0;
                    $result = mysql_query("SELECT * FROM screenings, venues WHERE screenings.venue_id = venues.venue_id AND screenings.film_id = '$in_film' AND screenings.type = 'f' ORDER BY screenings.date, screenings.time", $db);
                    	while ($myrow1 = mysql_fetch_array($result, MYSQL_ASSOC)) { 
                    	if($x == 0){$nolist = "'".$myrow1["screen_id"]."'"; $x++;}else{$nolist .= ", '$myrow1[screen_id]'"; $x++; }
                    	echo $myrow1["venue_name"]." $myrow1[date] $myrow1[time] - <a href='?id=$in_film&action=screening&site=$myrow1[screen_id]'>remove</a> <br />";
                    	 }
                    	 if($nolist != ''){$notin = " WHERE cat_id NOT IN ($nolist)";} ?>
				&nbsp;</dd>
			</dl>
			</fieldset>
			<?php } ?>
			
			 <fieldset><legend>Add Screening</legend> 
			
			<dl>
				<dt>Venue:</dt>
			
				<dd>
				<select name="venue" id="venue">
                    <option value="none">None</option>
                    
                    <?php
                    
                    $result = mysql_query("SELECT * FROM venues  ORDER BY venue_name", $db);
                    	while ($myrow2 = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
                    	<option value="<?php echo $myrow2["venue_id"];?>" <?php if($myrow2["venue_id"] == $in_venue){echo "selected";}?>><?php echo $myrow2["venue_name"];?></option>

                    	<?php }?>

                  </select>
                 </dd>
               </dl>  
                  
          <dl>
            <dt>Date:</dt>
            <dd><input type="date" name="date" id="date" value="<?php echo $date;?>"   /></dd>
          </dl>
          
          <dl>
            <dt>Time:</dt>
            <dd><input type="number" name="hour" id="hour" step="1" min="1" max="12" placeholder="hour" size="4" maxlength="2" /> :
            	<input type="number" name="min" id="min" step="1" min="0" max="60" placeholder="min" size="4" maxlength="2"   />
            	<select name="ampm" id="ampm" >
            		<option value="am">AM</option>
            		<option value="pm" selected >PM</option>
            	</select>
            	</dd>
          </dl>

		</fieldset>
		
	
		<fieldset><legend>Film Description</legend> 

            <h3>Film Description:</h3>
            <textarea name="editor1" id="editor1" class="ckeditor"><?php echo $myrow[description];?></textarea>
         
      <p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Update" /></p>
</fieldset>
    </form>	
		

	<?php if($myrow[video_embed] != ''){ ?>
	<h3>Current Video</h3>
	<?php echo $myrow[video_embed];
	} ?>	


    
		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->

   <script type="text/javascript">
	CKEDITOR.replace( 'editor1', {toolbar : 'MyToolbar'} );
	
	CKEDITOR.config.width = '700';
	CKEDITOR.config.height = '200';
	CKEDITOR.config.toolbarStartupExpanded = 'false';
	
</script>

</body> 
</html>